import { createStore, StoreOptions, Store } from 'vuex'
import persistedstate from './persistedstate'

/**
 * 初始化vuex4
 * @param props
 * @param initStore
 * @returns {*}
 */
export const initVuex = <T>(initStore: StoreOptions<T>, plugin?: any): Store<T> => {
  if (!initStore) {
    throw new Error('初始化vuex需要传入initStore');
  }
  const storeInstance = createStore<T>({
    ...initStore,
    plugins: plugin ? [plugin, persistedstate()] : [persistedstate()]
  })
  return storeInstance
}
